package itstudio.instructor.fragment;

import itstudio.app.wq.R;
import itstudio.instructor.adapter.DraftsListViewAdapter;
import itstudio.instructor.entity.News;
import itstudio.instructor.service.DatebaseService;
import itstudio.instructor.xlistview.XListView;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

/**
 * @Description 草稿 Fragment
 * 
 * @author MR.Wang
 * 
 * @date 2014-7-5 上午1:13:26
 * 
 * @version V1.0
 */

@SuppressLint("ValidFragment")
public class FragmentDrafts extends Fragment{

	private View rootView;
	private DraftsListViewAdapter adpater;
	View parentView;
	private XListView listView;
	DisplayImageOptions options;
	private List<News> newsList;
	public static FragmentDrafts mactivity;
	private LinearLayout emptylLayout;
	private LinearLayout listlLayout;
	private Context context;

	// 获取到下载url后，直接复制给MapApp,里面的全局变量

	private static FragmentDrafts singleton;

	public static FragmentDrafts getInstance(Context context) {
		if (singleton == null) {
			singleton = new FragmentDrafts();
		}
		return singleton;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initImageOptions();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initView(rootView);
	}
	private void initImageOptions() {
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.showImageOnLoading(R.drawable.user_image_load_loading)
				.showImageOnFail(R.drawable.user_image_load_fail)
				.cacheOnDisc(true).considerExifParams(true)
				// 设置下载的图片是否缓存在内存中
				.displayer(new FadeInBitmapDisplayer(200))
				.bitmapConfig(Bitmap.Config.ALPHA_8).build(); // 设置图片的解码类型
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mactivity = this;
		context = inflater.getContext();

		rootView = inflater.inflate(R.layout.fragment_drafts, container, false);
		initView(rootView);

		return rootView;

	}

	private void initView(View parentView) {
		// TODO Auto-generated method stub
		emptylLayout = (LinearLayout) parentView.findViewById(R.id.empty_layout);
		listlLayout=(LinearLayout) parentView.findViewById(R.id.list_layout);
		DatebaseService service = new DatebaseService(context);
		newsList = new ArrayList<News>();
		newsList=service.queryData();
		listView = (XListView) parentView.findViewById(R.id.listview_drafts);
		listView.setPullLoadEnable(true);
		listView.setPullRefreshEnable(false);// 取消下拉刷新

		// 获取屏幕分辨率
		DisplayMetrics dm = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(dm); 只能在activity中用
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		// 加载延迟
		adpater = new DraftsListViewAdapter(getActivity(), newsList, options,
				dm.widthPixels);
		AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(
				adpater);
		listView.setPullLoadEnable(false);
		listView.setPullRefreshEnable(false);// 取消下拉刷新
		animAdapter.setAbsListView(listView);
		animAdapter.setInitialDelayMillis(300);
		//listView.setXListViewListener(this);
		listView.setAdapter(animAdapter);
		if (listView.getCount()<=2) {
			listlLayout.setVisibility(View.GONE);
			emptylLayout.setVisibility(View.VISIBLE);
		}
	}

}
